<template>
    <div id="login">
        <el-row>
            <!-- logo -->
            <el-col :lg="{span:12,offset:6}" class="title">
                <img src="../../assets/image/login/logo.png">
            </el-col>
            <!-- 描述 -->
            <el-col :lg="{span:12,offset:6}" class="description">
                即刻登录 · 极致享受
            </el-col>
            <!-- 表单内容 -->
            <el-col :lg="{span:24}">
                <div class="login-page">
                    <div class="login-title">专业博客</div>
                    <form>
                        <el-input v-model="userName" placeholder="用户名"></el-input>
                        <el-input placeholder="密码" v-model="password" show-password></el-input>
                        <div class="button">
                            <el-button type="primary" class="login" @click="login">登录</el-button>
                            <el-button type="primary" class="register" @click="turntoRegister">注册</el-button>
                        </div>
                    </form>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {

        name: "login",
        data(){
            return{
                userName:'',
                password:''
            }
        },
        methods:{
            turntoRegister(){
                this.$router.push('/register');
            },
            login(){
                if(!(this.userName!=''&&this.password!='')){
                    this.$message("请输入账号与密码!");
                    return;
                }
                let url = 'http://localhost:8080/user/login'
                axios.post(url,{userName:this.userName,userPassword:this.password}).then(res=>{
                    if(res.data['message']){
                        this.$message(res.data['message']);
                        return;
                    }
                    let token = res.data['token'];//token
                    this.$store.commit('setToken',token);
                    //console.log(window.localStorage['token']);  可以直接获取token值
                    //console.log(this.$store.state.token);
                    window.localStorage.setItem("username",this.userName);
                    this.$router.replace('/home');
                }).catch(err=>{
                    console.log(err);
                })
            }
        },
        mounted(){
            document.querySelector('body').setAttribute('style', 'background-image: url('+require('@/assets/image/login/bac.jpg')+')');
            document.querySelector('.tabbar').setAttribute('style','display:none');
            document.querySelector('.gfooter').setAttribute('style','display:none');
        },
        beforeDestroy() {
            document.querySelector('body').removeAttribute('style');
            // document.querySelector('.tabbar').removeAttribute('display');
            // document.querySelector('.gfooter').removeAttribute('display');
        },
    }
</script>

<style scoped>
#login{
    width: 50%;
    margin-left: 25%;
    text-align: center;
    margin-top: 50px;
    padding-top: 50px;
}
.title{
    height: 80px;
    margin-bottom: 20px;
}
.title img {
    width: 160px;
    height: auto;
    margin-bottom: 10px;
}

.description{
    height: 40px;
    text-align: center;
    line-height: 40px;
    color: #dff0debf;
    font-weight: 200;
    margin-bottom: 20px;
    font-size: 13px;
}
.login-page{
    height: 230px;
    width: 56%;
    margin-left: 22%;
    padding-top: 20px;
    background-color: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    margin-bottom: 50px;
}
.login-title{
    text-align: center;
    color: white;
    font-weight: 400;
    font-size: 18px;
}
.el-input{
    width: 80%;
    margin-top: 20px;
}
.el-input >>> .el-input__inner{
    background-color: rgba(255,255,255,.1);
}
/deep/input::-webkit-input-placeholder {
    color: white;
}
.button{
    display: flex;
    justify-content: space-around;
    padding-top: 20px;
}
.el-button{
    width: 28%;
}
.el-button--primary{
    background-color: #2675b9a8;
    border-color: #2e6da400;
}
</style>